package com.steven.im.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;
import com.steven.im.App;
import com.steven.im.R;
import com.steven.im.entity.BaseEntity;
import com.steven.im.entity.MycollectEntity;
import com.steven.im.net.MyCallback;
import com.steven.im.net.Task;
import com.steven.im.utils.GsonUtils;
import com.steven.im.utils.ToastUtil;

import java.util.List;

import retrofit2.Response;

/**
 * 我的收藏
 */
public class MycollectAdapter extends BaseQuickAdapter<MycollectEntity, BaseViewHolder> {

    private Context mContext;
    private List<MycollectEntity> data;
    private MycollectAdapter adapter;
    private int position;

    public MycollectAdapter(Context ctx, @Nullable List<MycollectEntity> data) {
        super(R.layout.collection_item, data);
        this.adapter = this;
        this.mContext = ctx;
        this.data = data;

    }

    @Override
    protected void convert(BaseViewHolder helper, final MycollectEntity item) {
        if (((helper.getPosition() + 1) % 2) == 0) {
            //表示偶数
            setVisible(helper, item, true);
        } else {
            //表示奇数
            setVisible(helper, item, false);
        }
        position = helper.getPosition();
        helper.getView(R.id.ll_heart).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelCollection(item.getFavoriteId());
            }
        });
    }

    //设置 显示情况

    @SuppressLint("SetTextI18n")
    private void setVisible(final BaseViewHolder helper, MycollectEntity item, boolean visible) {
        if (visible) {
            helper.getView(R.id.left_good).setBackgroundResource(R.drawable.home_right_shape);
        } else {
            helper.getView(R.id.left_good).setBackgroundResource(R.drawable.home_left_shape);
        }
        Picasso.with(App.getContext()).load(item.getPreviewImage()).into(((ImageView) helper.getView(R.id.left_goodImg)));
        ((TextView) helper.getView(R.id.left_goodName)).setText(item.getGoodsTitle());
        ((TextView) helper.getView(R.id.left_goodM2)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        ((TextView) helper.getView(R.id.left_goodM1)).setText(item.getDiscountPric());
        ((TextView) helper.getView(R.id.left_goodM2)).setText(item.getOriginalPrice());
    }

    //设置 竞拍显示
    @SuppressLint("ResourceAsColor")
    private void setBid(String status, BaseViewHolder helper) {
        switch (status) {
            case "10"://预售中
                ((TextView) helper.getView(R.id.tv_status)).setText("预");
                helper.getView(R.id.tv_status).setBackgroundResource(R.drawable.bid_pre);
                helper.getView(R.id.tv_bidding).setBackgroundResource(R.color.colorFFDFCA);
                break;
            case "20"://竞拍中
                ((TextView) helper.getView(R.id.tv_status)).setText("拍");
                helper.getView(R.id.tv_status).setBackgroundResource(R.drawable.bid_pat);
                helper.getView(R.id.tv_bidding).setBackgroundResource(R.color.colorFFDBD4);
                break;
            case "30"://已售罄
                ((TextView) helper.getView(R.id.tv_status)).setText("罄");
                helper.getView(R.id.tv_status).setBackgroundResource(R.drawable.bid_stop);
                helper.getView(R.id.tv_bidding).setBackgroundResource(R.color.colorEEE);
                break;
            case "40"://已下架
                break;
        }
    }

    //取消收藏
    private void cancelCollection(String favoriteId) {
        Task.getApiService().cancelCollection(favoriteId).enqueue(new MyCallback<BaseEntity>((Activity) mContext) {
            @Override
            protected void onSuccess(Response<BaseEntity> response) {
                ToastUtil.showShort(mContext, response.body().getMessage());
                data.remove(position);
                getCollection();
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }

    //获取收藏
    private void getCollection() {
        Task.getApiService().getMyFavList().enqueue(new MyCallback<BaseEntity>((Activity) mContext) {
            @Override
            protected void onSuccess(Response<BaseEntity> response) {
                try {

                    if ("0".equals(response.body().getCode())) {
                        List<MycollectEntity> favList = GsonUtils.jsonToArrayList(
                                response.body().getResult().getJSONArray("favoriteList").toString(),
                                MycollectEntity.class);
                        data.clear();
                        data.addAll(favList);
                        adapter.notifyDataSetChanged();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }
}
